{template 'public/header'}
{template 'public/comhead'}
{if $operation == 'display'}
<div class="panel panel-default">
    <div class="panel-body text-danger">
    代金券适用于当用户下单满足指定条件时，商家给予订单指定额度的减免优惠。例如订单总额满100元，优惠5元。<br/>
    商品赠送适用于当用户下单满足指定条件时，商家给予订单指定的商品赠送。例如订单总额满100元，赠送两罐可乐。
    </div>
</div>
<div class="main">
    <form action="" method="post" class="form-horizontal form">
        <div class="panel panel-default">
            <div class="panel-body">
                <div class="row-fluid">
                    <div class="span8 control-group">
                        <a class="btn btn-success"
                           href="{php echo $this->createWebUrl('coupon', array('op' => 'post', 'storeid' => $storeid))}"><i
                                class="fa fa-plus"></i> 添加优惠券</a>
                        <a class="btn btn-default" href="{php echo $this->createWebUrl('coupon', array('op' => 'display', 'storeid' => $storeid))}">全部</a>
                        <a class="btn btn-default" href="{php echo $this->createWebUrl('coupon', array('op' => 'display', 'attrtype' => 1, 'storeid' => $storeid))}">消费券({$type_count1})</a>
                        <a class="btn btn-default" href="{php echo $this->createWebUrl('coupon', array('op' => 'display', 'attrtype' => 2, 'storeid' => $storeid))}">营销券({$type_count2})</a>
                    </div>
                </div>
            </div>
        </div>
        <div class="panel panel-default">
            <div class="table-responsive panel-body">
                <table class="table table-hover">
                    <thead class="navbar-inner">
                    <tr>
                        <th style="width:10%;">显示顺序</th>
                        <th style="width:28%;">名称</th>
                        <th style="width:10%;">类型</th>
                        <th style="width:10%;">属性</th>
                        <th style="width:8%;">状态</th>
                        <th style="width:25%;">时间</th>
                        <th style="width:19%;"></th>
                    </tr>
                    </thead>
                    <tbody id="level-list">
                    {loop $coupons $coupon}
                    <tr>
                        <td><input type="text" class="form-control" name="displayorder[{$coupon['id']}]"
                                   value="{$coupon['displayorder']}"></td>
                        <td>{$coupon['title']}</td>
                        <td>
                            <span class="label label-success">{$coupon_type[$coupon['type']]}</span>
                        </td>
                        <td>
                            <span class="label label-success">{$coupon_attr_type[$coupon['attr_type']]}</span>
                        </td>
                        <td>
                            {if TIMESTAMP<$coupon['starttime']}
                            <span class="label label-danger">未开始</span>
                            {elseif TIMESTAMP>$coupon['starttime'] && TIMESTAMP<$coupon['endtime']}
                            <span class="label label-success">进行中</span>
                            {else}
                            <span class="label label-danger">已结束</span>
                            {/if}
                        </td>
                        <td>
                            开始时间：{php echo date('Y-m-d H:i:s', $coupon[starttime]);}<br/>
                            结束时间：{php echo date('Y-m-d H:i:s', $coupon[endtime]);}
                        </td>
                        <td>
                            <a class="btn btn-default btn-sm"
                               href="{php echo $this->createWebUrl('sncodelist', array('op' => 'display', 'couponid' => $coupon['id'], 'storeid' => $storeid))}"><i class="fa fa-bar-chart"> 记录</i></a>
                            <a class="btn btn-success btn-sm" href="{php echo $this->createWebUrl('coupon', array('op' => 'post', 'id' => $coupon['id'], 'storeid' => $storeid))}"><i class="fa fa-pencil"> 编辑</i></a>
                            <a class="btn btn-danger btn-sm" onclick="return confirm('确认删除吗？');return false;"
                               href="{php echo $this->createWebUrl('coupon', array('op' => 'delete', 'id' => $coupon['id'], 'storeid' => $storeid))}"><i class="fa fa-times"> 删除</i></a>
                        </td>
                    </tr>
                    {/loop}
                    </tbody>
                    <tfoot>
                    <tr>
                        <td colspan="7">
                            <input name="submit" type="submit" class="btn btn-primary" value="批量排序">
                            <input type="hidden" name="token" value="{$_W['token']}"/>
                        </td>
                    </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </form>
    {$pager}
</div>
{else if $operation == 'post'}
<div class="main">
    {if !empty($reply['id'])}
    <div class="panel panel-default account">
        <div class="panel-body">
            <p style="margin: 0px"><strong>活动链接 :</strong> <a href="javascript:;" title="点击复制Token">{php echo $_W['siteroot'] . 'app/index.php?i=' . $reply['weid'] . '&c=entry&id=' . $reply['id'] . '&do=coupon&m=weisrc_dish'}</a></p>
        </div>
    </div>
    <script>
        require(['jquery', 'util'], function($, u){
            $('.account p a').each(function(){
                u.clip(this, $(this).text());
            });
        });
    </script>
    {/if}
    <form action="" method="post" onsubmit="return check();" class="form-horizontal form" enctype="multipart/form-data">
        <input type="hidden" id="id" name="id" value="{$reply['id']}"/>
        <div class="panel panel-default">
            <div class="panel-heading">
                {$title}
            </div>
            <div class="panel-body">
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">名称</label>
                    <div class="col-sm-9">
                        <input type="text" name="title" value="{$reply['title']}" id="title" class="form-control" placeholder="请输入优惠券名称"/>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">属性</label>
                    <div class="col-sm-9">
                        <label class="radio-inline">
                            <input type="radio" name="attr_type" value="1" {if empty($reply)}checked="checked"{elseif $reply['attr_type']==1}checked="checked"{/if} />消费券
                        </label>
                        <!--<label class="radio-inline">-->
                            <!--<input type="radio" name="attr_type" value="2" {if $reply['attr_type']==2}checked="checked"{/if} />营销券-->
                        <!--</label>-->
                        <!--<div class="help-block"><code>选择营销券可以用于积分兑换</code></div>-->
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">期限</label>
                    <div class="col-sm-9">
                        {php echo tpl_form_field_daterange('datelimit', array('starttime'=>$starttime,'endtime' =>$endtime), true)}
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">类型</label>
                    <div class="col-sm-9">
                        <label class="radio-inline">
                            <input type="radio" name="type" value="1" {if empty($reply)}checked="checked"{elseif
                                   $reply['type']==1}checked="checked"{/if} />商品赠送
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="type" value="2" {if $reply['type']==2}checked="checked"{/if} />代金券
                        </label>
                    </div>
                </div>
                <!--<div class="form-group">-->
                    <!--<label class="col-xs-12 col-sm-3 col-md-2 control-label">使用规则</label>-->
                    <!--<div class="col-sm-9">-->
                        <!--<label class="radio-inline">-->
                            <!--<input type="radio" name="ruletype" value="1" {if empty($reply)}checked="checked"{elseif-->
                                   <!--$reply['ruletype']==1}checked="checked"{/if} />不限-->
                        <!--</label>-->
                        <!--<label class="radio-inline">-->
                            <!--<input type="radio" name="ruletype" value="2" {if $reply['ruletype']==2}checked="checked"{/if} />首单-->
                        <!--</label>-->
                    <!--</div>-->
                <!--</div>-->
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">消费金额满</label>
                    <div class="col-sm-9">
                        <input type="text" name="gmoney" value="{if empty($reply['gmoney'])}0.00{else}{$reply['gmoney']}{/if}" id="gmoney" class="form-control" />
                        <span class="help-block"><code>消费时使用条件,0表示没限制</code></span>
                    </div>
                </div>
                <div class="form-group type2" style="{if empty($reply) || $reply['type']!=2}display: none;{/if}">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">抵用金额</label>
                    <div class="col-sm-9">
                        <input type="text" name="dmoney" value="{if empty($reply['dmoney'])}0.00{else}{$reply['dmoney']}{/if}" id="dmoney" class="form-control" />
                    </div>
                </div>
                <div class="form-group attr_type2" style="{if $reply['attr_type']==2}display: none;{/if}">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">参与名额数量</label>
                    <div class="col-sm-9">
                        <input type="text" name="totalcount" value="{if empty($reply['totalcount'])}0{else}{$reply['totalcount']}{/if}" id="totalcount" class="form-control" />
                        <div class="help-block"><code>不填写则没有名额数量限制</code></div>
                    </div>
                </div>
                <div class="form-group attr_type2" style="{if $reply['attr_type']==2}display: none;{/if}">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">用户可领取数量</label>
                    <div class="col-sm-9">
                        <input type="text" name="usercount" value="{if empty($reply['usercount'])}0{else}{$reply['usercount']}{/if}" id="usercount" class="form-control" />
                        <div class="help-block"><code>不填写则没有数量限制</code></div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">优惠券图片</label>
                    <div class="col-sm-9">
                        {if empty($thumb)}
                            {php echo tpl_form_field_image('thumb','../addons/weisrc_dish/template/images/coupon.jpg')}
                        {else}
                            {php echo tpl_form_field_image('thumb', $reply['thumb'])}
                        {/if}
                        <div class="help-block">大图片建议尺寸：720像素 * 400像素</div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">使用说明</label>
                    <div class="col-sm-9">
                        <textarea style="height:200px;" class="richtext form-control" name="content" id="content">{$reply['content']}</textarea>
                        <div class="help-block">在此说明券的使用方式，如最低消费金额，优惠券打折信息，不与其他优惠同时使用、节假日不可使用等。</div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">排序</label>
                    <div class="col-sm-9">
                        <input type="text" name="displayorder" value="{if empty($reply)}0{else}{$reply['displayorder']}{/if}" id="displayorder" class="form-control" />
                    </div>
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-12">
                <input name="submit" type="submit" value="保存设置" class="btn btn-primary span3">
                <input type="hidden" name="token" value="{$_W['token']}"/>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript">
    $(function () {
        var property = ""
        $(":radio[name='attr_type']").click(function () {
            var $this = $(this);
            if(2 == $(this).val()){
                $(".attr_type2").hide();
            } else {
                $(".attr_type2").show();
            }
        });

        $(":radio[name='type']").click(function () {
            var $this = $(this);
            if(2 == $(this).val()){
                $(".type2").show();
                $(".type").hide();
            } else {
                $(".type2").hide();
                $(".type").show();
            }
        });
    });

    function check() {
        if($.trim($('#title').val()) == '') {
            message('没有输入标题.', '', 'error');
            return false;
        }
        if($.trim($('#title').val()).length > 30) {
            message('标题不能多于30个字.', '', 'error');
            return false;
        }
        var count = $.trim($('#count').val());
        if(count == '') {
            message('没有输入优惠券张数.', '', 'error');
            return false;
        }
        if(isNaN(count)){
            message('优惠券张数必须为数字.', '', 'error');
            return false;
        }
        return true;
    }
</script>
<script type="text/javascript">
    require(['jquery', 'util'], function ($, u) {
        $(function () {
            u.editor($('.richtext')[0]);
        });
    });
</script>
{/if}
{template 'common/footer'}
